package com.itheima.service.system.Impl;

import com.github.pagehelper.PageHelper;
import com.itheima.domain.system.User;
import com.itheima.mapper.system.UserMapper;
import com.itheima.service.system.UserService;
import com.itheima.utils.MapperUtil;
import com.itheima.utils.UidUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @param
 * @return
 */
public class UserServiceImpl implements UserService {
    @Override
    public List<User> findByPage(int pageNum, int pageSize) {
        //定义返回值
        List<User> list=null;
        //工具类走起
        SqlSession session = MapperUtil.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        //在执行接口代理的方法查询所有之前先设置好分页参数
        PageHelper.startPage(pageNum,pageSize);
        //调用代理对象的方法
        list= mapper.findAll();
        //释放资源
        MapperUtil.close(session);
        return list;
    }

    @Override
    public int save(User user) {
        //定义返回值
        int i=0;
        //工具类走起
        SqlSession session = MapperUtil.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        //获取唯一id
        String uid = UidUtil.getUid();
        user.setId(uid);
        //调用接口中的新增方法
        i=mapper.save(user);
        //释放资源
        MapperUtil.close(session);
        return i;
    }
}
